/**
 * @作者 zw
 * @微信公号 搬砖暗夜码农
 * @开源项目 $ https://gitee.com/zhouwei5208888/spring-boot-distributed
 * @我的博客 $ http://itzhouwei.com
 * @创建时间 2025/1/20 10:23
 */
package com.itzhouwei.datasource.dynamic.controller;

import com.itzhouwei.datasource.dynamic.aop.DS;
import com.itzhouwei.datasource.dynamic.domain.entity.TestUser;
import com.itzhouwei.datasource.dynamic.mapper.TestUserMapper;
import com.itzhouwei.datasource.dynamic.util.DataSourceContextHolder;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName：DynamicDataSourceTest
 * @Author: zw，微信公共号：搬砖暗夜码农
 * @Date: 2025/1/20 10:23
 * @Description:
 * @Copyright：公众号：搬砖暗夜码农 | 博客：https://itzhouwei.com - 沉淀、学习、分享、成长，让自己和他人都能有所收获！
 */
@RestController
@RequestMapping("/zw/test")
public class DynamicDataSourceTest {

    @Resource
    private TestUserMapper testUserMapper;

    @GetMapping("/getData.do/{datasourceName}")
    public String getMasterData(@PathVariable("datasourceName") String datasourceName){
        DataSourceContextHolder.setDataSource(datasourceName);
        TestUser testUser = testUserMapper.selectOne(null);
        DataSourceContextHolder.removeDataSource();
        return testUser.getUserName();
    }

    @GetMapping("/getMasterData.do")
    public String getMasterData(){
        TestUser testUser = testUserMapper.selectOne(null);
        return testUser.getUserName();
    }

    @GetMapping("/getSlaveData.do")
    @DS("slave")
    public String getSlaveData(){
        TestUser testUser = testUserMapper.selectOne(null);
        return testUser.getUserName();
    }

}
